package com.huawei.hms.support.hianalytics;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import com.huawei.hianalytics.hms.HiAnalytics;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.common.internal.RequestHeader;
import com.huawei.hms.common.internal.ResponseHeader;
import com.huawei.hms.common.internal.TransactionIdCreater;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Util;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONException;
import org.json.JSONObject;

public class HiAnalyticsUtil {
    private static final Object a = new Object();
    private static final Object b = new Object();
    private static HiAnalyticsUtil c;
    private int d = 0;

    public static HiAnalyticsUtil getInstance() {
        HiAnalyticsUtil hiAnalyticsUtil;
        synchronized (a) {
            if (c == null) {
                c = new HiAnalyticsUtil();
            }
            hiAnalyticsUtil = c;
        }
        return hiAnalyticsUtil;
    }

    public void onEvent(Context context, String str, Map<String, String> map) {
        Object a = a(map);
        if (!TextUtils.isEmpty(a)) {
            onEvent2(context, str, a);
            HiAnalytics.onEvent(1, str, b(map));
            a();
        }
    }

    public void onEvent(Context context, String str, String str2) {
        if (!getInstance().hasError() && context != null) {
            onEvent2(context, str, a(context, str2));
        }
    }

    public void onBuoyEvent(Context context, String str, String str2) {
        if (!getInstance().hasError() && context != null) {
            onEvent2(context, str, str2);
        }
    }

    public void onEvent2(Context context, String str, String str2) {
        if (context != null && HiAnalytics.getInitFlag()) {
            HiAnalytics.onEvent(context, str, str2);
        }
    }

    private String a(Context context, String str) {
        return "01|" + "" + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + context.getPackageName() + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + Util.getAppId(context) + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + 30003300 + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + str;
    }

    private String a(Map<String, String> map) {
        if (map == null) {
            return null;
        }
        JSONObject jSONObject = new JSONObject();
        try {
            for (Entry entry : map.entrySet()) {
                jSONObject.put((String) entry.getKey(), entry.getValue());
            }
        } catch (JSONException e) {
            HMSLog.e("HiAnalyticsUtil", "AnalyticsHelper create json exception" + e.getMessage());
        }
        return jSONObject.toString();
    }

    public boolean hasError() {
        if (Util.isChinaROM()) {
            return false;
        }
        HMSLog.i("HiAnalyticsUtil", "not ChinaROM ");
        return true;
    }

    public void onNewEvent(Context context, String str, Map map) {
        if (!hasError()) {
            String a = a(map);
            if (context != null && HiAnalytics.getInitFlag()) {
                HiAnalytics.onEvent(context, str, a);
                HiAnalytics.onEvent(1, str, b(map));
                a();
            }
        }
    }

    @Deprecated
    public Map<String, String> getMapForBi(Context context, String str) {
        Map<String, String> hashMap = new HashMap();
        String[] split = str.split("\\.");
        if (split.length >= 2) {
            Object obj = split[0];
            Object obj2 = split[1];
            String appId = Util.getAppId(context);
            hashMap.put("transId", TransactionIdCreater.getId(appId, str));
            hashMap.put(RpkInfo.APPID, appId);
            hashMap.put(HiAnalyticsConstant.BI_KEY_SERVICE, obj);
            hashMap.put("apiName", obj2);
            hashMap.put(HiAnalyticsConstant.BI_KEY_PACKAGE, context.getPackageName());
            hashMap.put("version", HuaweiApiAvailability.HMS_SDK_VERSION_NAME);
            hashMap.put("callTime", String.valueOf(System.currentTimeMillis()));
        }
        return hashMap;
    }

    public static Map<String, String> getMapFromRequestHeader(ResponseHeader responseHeader) {
        Map hashMap = new HashMap();
        if (responseHeader != null) {
            hashMap.put("transId", responseHeader.getTransactionId());
            hashMap.put(RpkInfo.APPID, responseHeader.getActualAppID());
            hashMap.put(HiAnalyticsConstant.BI_KEY_SERVICE, responseHeader.getSrvName());
            Object apiName = responseHeader.getApiName();
            if (!TextUtils.isEmpty(apiName)) {
                String[] split = apiName.split("\\.");
                if (split.length >= 2) {
                    hashMap.put("apiName", split[1]);
                }
            }
            hashMap.put(HiAnalyticsConstant.BI_KEY_PACKAGE, responseHeader.getPkgName());
            hashMap.put("statusCode", String.valueOf(responseHeader.getStatusCode()));
            hashMap.put(HiAnalyticsConstant.BI_KEY_RESUST, String.valueOf(responseHeader.getErrorCode()));
            hashMap.put("callTime", String.valueOf(System.currentTimeMillis()));
            hashMap.put("baseVersion", String.valueOf(30003300));
        }
        return hashMap;
    }

    public static Map<String, String> getMapFromForegroundResponseHeader(ResponseHeader responseHeader) {
        Map hashMap = new HashMap();
        if (responseHeader != null) {
            hashMap.put("transId", responseHeader.getTransactionId());
            hashMap.put(RpkInfo.APPID, responseHeader.getActualAppID());
            hashMap.put(HiAnalyticsConstant.BI_KEY_SERVICE, responseHeader.getSrvName());
            hashMap.put("apiName", responseHeader.getApiName());
            hashMap.put(HiAnalyticsConstant.BI_KEY_PACKAGE, responseHeader.getPkgName());
            hashMap.put("statusCode", String.valueOf(responseHeader.getStatusCode()));
            hashMap.put(HiAnalyticsConstant.BI_KEY_RESUST, String.valueOf(responseHeader.getErrorCode()));
            hashMap.put("callTime", String.valueOf(System.currentTimeMillis()));
            hashMap.put("baseVersion", String.valueOf(30003300));
        }
        return hashMap;
    }

    public Map<String, String> getMapFromRequestHeader(RequestHeader requestHeader) {
        Map hashMap = new HashMap();
        if (requestHeader != null) {
            hashMap.put("transId", requestHeader.getTransactionId());
            hashMap.put(RpkInfo.APPID, requestHeader.getActualAppID());
            hashMap.put(HiAnalyticsConstant.BI_KEY_SERVICE, requestHeader.getSrvName());
            Object apiName = requestHeader.getApiName();
            if (!TextUtils.isEmpty(apiName)) {
                String[] split = apiName.split("\\.");
                if (split.length >= 2) {
                    hashMap.put("apiName", split[1]);
                }
            }
            hashMap.put(HiAnalyticsConstant.BI_KEY_PACKAGE, requestHeader.getPkgName());
            hashMap.put("callTime", String.valueOf(System.currentTimeMillis()));
            hashMap.put("baseVersion", String.valueOf(30003300));
        }
        return hashMap;
    }

    public Map<String, String> getMapFromForegroundRequestHeader(RequestHeader requestHeader) {
        Map hashMap = new HashMap();
        if (requestHeader != null) {
            hashMap.put("transId", requestHeader.getTransactionId());
            hashMap.put(RpkInfo.APPID, requestHeader.getActualAppID());
            hashMap.put(HiAnalyticsConstant.BI_KEY_SERVICE, requestHeader.getSrvName());
            hashMap.put("apiName", requestHeader.getApiName());
            hashMap.put(HiAnalyticsConstant.BI_KEY_PACKAGE, requestHeader.getPkgName());
            hashMap.put("callTime", String.valueOf(System.currentTimeMillis()));
            hashMap.put("baseVersion", String.valueOf(30003300));
        }
        return hashMap;
    }

    private static LinkedHashMap<String, String> b(Map<String, String> map) {
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
        for (String str : map.keySet()) {
            linkedHashMap.put(str, map.get(str));
        }
        return linkedHashMap;
    }

    private void a() {
        synchronized (b) {
            if (this.d < 60) {
                this.d++;
            } else {
                this.d = 0;
                HiAnalytics.onReport();
            }
        }
    }
}
